package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcAllergyEntity;
import com.dhcc.cdc.busi.service.ICdcAllergyService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 过敏记录
 * 
 * @ClassName: CdcAllergyController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:09
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcallergy")
public class CdcAllergyController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcallergy/";

	@Autowired
	private ICdcAllergyService cdcAllergyService;
	

    /**
     * 跳转到过敏记录列表首页
     */
    @RequestMapping("")
    @Permission("cdcallergy:list")
    public String index() {
        return PREFIX + "cdcallergy.html";
    }
	
    /**
     * 跳转到新增过敏记录
     */
    @RequestMapping("/to_add")
    @Permission("cdcallergy:add")
    public String toAdd() {
        return PREFIX + "cdcallergy_add.html";
    }
	
	/**
     * 跳转到修改过敏记录
     */
    @RequestMapping("/to_update/{cdcAllergyId}")
    @Permission("cdcallergy:update")
    public String toUpdate(@PathVariable Long cdcAllergyId, Model model) {
        CdcAllergyEntity cdcAllergy = this.cdcAllergyService.selectById(cdcAllergyId);
        model.addAttribute("cdcAllergy",cdcAllergy);
        return PREFIX + "cdcallergy_edit.html";
    }

    /**
     * 获取过敏记录列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcallergy:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcAllergyEntity> page = new PageFactory<CdcAllergyEntity>().defaultPage();
        List<CdcAllergyEntity> result = this.cdcAllergyService.page(page,params);
        page.setRecords((List<CdcAllergyEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增过敏记录
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcallergy:add")
    @BussinessLog(value = "新增过敏记录")
    public Object add(CdcAllergyEntity cdcAllergy) {
        if (CommonUtil.isOneEmpty(cdcAllergy)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcAllergy.insert();
        return R.ok();
    }

    /**
     * 删除过敏记录
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcallergy:delete")
    @BussinessLog(value = "删除过敏记录")
    public Object delete(@RequestParam Long cdcAllergyId) {
	
        this.cdcAllergyService.deleteById(cdcAllergyId);

        return R.ok();
    }

    /**
     * 修改过敏记录
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcallergy:update")
    @BussinessLog(value = "修改过敏记录")
    public Object update(CdcAllergyEntity cdcAllergy) {
        if (CommonUtil.isOneEmpty(cdcAllergy)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcAllergyEntity old = this.cdcAllergyService.selectById(cdcAllergy.getId());

        old.setEpisodeId(cdcAllergy.getEpisodeId());
        old.setCode(cdcAllergy.getCode());
        old.setDescription(cdcAllergy.getDescription());
        old.setAlleDate(cdcAllergy.getAlleDate());
        old.setDataId(cdcAllergy.getDataId());
        old.setIsActive(cdcAllergy.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
